package cn.chaohi.config;

import cn.chaohi.common.ApiException;
import cn.chaohi.common.CodeEnum;
import cn.chaohi.model.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Mr.zhou
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception ex) {
        ex.printStackTrace();
        return new Result(CodeEnum.INTERNAL_SERVER_ERROR);
    }



    @ResponseBody
    @ExceptionHandler(value = ApiException.class)
    public Result apiExceptionHandler(ApiException ex) {
        return ex.getResult();
    }
}
